package cn.ydt.ydt.work.webapi.controller;

import cn.ydt.ydt.common.resful.JsonPage;
import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.pojo.work.dto.WorkDTO;
import cn.ydt.ydt.pojo.work.vo.WorkDetailsVO;
import cn.ydt.ydt.pojo.work.vo.WorkSimpleVO;
import cn.ydt.ydt.qrcode.constant.WebConst;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.IWorkService;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/work", produces = "application/json; charset=utf-8")
public class WorkController {
    @Autowired
    private IWorkService iWorkService;

    //增加任务
    @PostMapping("/addWork")
    @ApiOperation(value = "增加任务")
    public JsonResult<WorkDTO> addNew(@Valid WorkDTO workDTO) {
        iWorkService.addNewWork(workDTO);
        return JsonResult.ok(workDTO, "增加任务成功！");
    }

    /**
     * 删除任务
     */
    @ApiOperation(value = "删除任务")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId", value = "任务id", paramType = "path", required = true, dataType = "Long")
    })
    public JsonResult<Void> deleteById(@Valid Long workId) {
        iWorkService.deleteByWorkID(workId);
        return JsonResult.ok("删除任务成功！");
    }

    /**
     * 删除全部已完成任务
     */
    @ApiOperation(value = "删除已完成任务")
    @PostMapping("/deleteFinish")
    public JsonResult<Void> deleteFinish() {
        iWorkService.deleteAllFinishWork();
        return JsonResult.ok("删除已完成任务成功！");
    }

    /**
     * 完成任务
     */
    @ApiOperation(value = "完成任务")
    @PostMapping("/finish")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId", value = "任务id", paramType = "path", required = true, dataType = "Long")
    })
    public JsonResult<Void> finishById(@Valid Long workId) {
        iWorkService.finish(workId);
        return JsonResult.ok("完成任务成功！");
    }

    /**
     * 分配任务
     */
    @ApiOperation(value = "分配任务")
    @GetMapping("/give/{workId}/{volunteerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId", value = "任务id", paramType = "path", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "volunteerId", value = "志愿者id", paramType = "path", required = true, dataType = "Long")
    })
    public JsonResult<Void> giveById(@Valid @PathVariable Long workId, @Valid @PathVariable Long volunteerId) {
        iWorkService.giveWork(workId, volunteerId);
        return JsonResult.ok("分配任务成功！");
    }

    /**
     * 根据workId查询任务
     */
    @GetMapping("/getByWorkId")
    @ApiOperation(value = "简单查询任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId", value = "任务id", paramType = "path", required = true, dataType = "Long")
    })
    public JsonResult<WorkSimpleVO> getByWorkId(@Valid Long workId) {
        return JsonResult.ok(iWorkService.getByWorkId(workId), "查询成功！");
    }

    /**
     * 根据workId查询任务详情
     */
    @GetMapping("/getDetailsByWorkId/{workId}")
    @ApiOperation(value = "详细查询任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workId", value = "任务id", paramType = "path", required = true, dataType = "Long")
    })
    public JsonResult<WorkDetailsVO> getDetailsByWorkId(@PathVariable Long workId) {
        return JsonResult.ok(iWorkService.getDetailsWork(workId), "查询成功！");
    }

    /**
     * 查询任务列表
     */
    @ApiOperation(value = "查询任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @GetMapping("/list-by-work/{page}")
    public JsonResult<JsonPage<WorkDetailsVO>> listByWork(
            @PathVariable(required = false) Integer page,
            @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<WorkDetailsVO> workDetailsVOJsonPage = iWorkService.listByWork(page, pageSize);
        return JsonResult.ok(workDetailsVOJsonPage);
    }

    /**
     * 查询未完成任务列表
     */
    @ApiOperation(value = "查询未完成任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @GetMapping("/list-not-work/{page}")
    public JsonResult<JsonPage<WorkDetailsVO>> listNotByWork(
            @PathVariable(required = false) Integer page,
            @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<WorkDetailsVO> workDetailsVOJsonPage = iWorkService.listNotFinishByWork(page, pageSize);
        return JsonResult.ok(workDetailsVOJsonPage);
    }



    /**
     * 根据用户id查询类别列表
     */
    @ApiOperation(value = "根据userId查询类别列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    @GetMapping("/list-byuserId-work")
    public JsonResult<JsonPage<WorkSimpleVO>> listWorkByUserId(
            @RequestParam(required = true) Long userId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<WorkSimpleVO> workSimpleVOJsonPage = iWorkService.listByUserId(userId, page, pageSize);
        return JsonResult.ok(workSimpleVOJsonPage);
    }

    @GetMapping("/listByVolunteerId")
    public JsonResult<List<WorkDetailsVO>> listVolunteerId(){
        List<WorkDetailsVO> listVolunteer = iWorkService.listByVolunteerId();
        return JsonResult.ok(listVolunteer);
    };
}
